import { Component, OnInit } from '@angular/core';
import { DataVisualizationService } from '../dataVisualization.service';
import { TranslateService } from '@ngx-translate/core';
import { Router } from '@angular/router';
import { ToolsService } from 'app/shared/service';

@Component({
    selector: 'itmp-equipment-analytics',
    templateUrl: './equipment-analytics.component.html',
    styleUrls: ['./equipment-analytics.component.css']
})
export class EquipmentAnalyticsComponent implements OnInit {
    vmsData: any;
    videoDetector: any;
    signalController: any;
    CCTVData: any;
    Alarm: any;
    TrafficAlert: any;
    constructor(
        private ds: DataVisualizationService,
        private translate: TranslateService,
        private router: Router,
        private toolService: ToolsService
    ) {}

    ngOnInit() {
        this.translate.onLangChange.subscribe(() => this._getData());
        this._getData();
    }
    _setData(value, name) {
        const tempTranslate = ['Normal', 'Failure'];
        const translateData = [];
        tempTranslate.forEach(item => {
            this.translate.get('Data_Visualization.Overall.' + item).subscribe((res: string) => {
                translateData.push(this.toolService.getCapitalLetters(res));
            });
        });
        setTimeout(() => {
            this[name] = [{ value: value.Normal, name: translateData[0] }, { value: value.Failure, name: translateData[1] }];
        });
    }
    _getData() {
        this.ds.getIncidentAnalyticsReport(200).subscribe(res => {
            if (res.messagecode === '000000') {
                this.Alarm = res.data[0].Alarm;
                this.TrafficAlert = res.data[0].TrafficAlert;
                res.data.forEach(item => {
                    switch (item.selectedType) {
                        case 'VMS':
                            this._setData(item, 'vmsData');
                            break;
                        case 'Video Detector':
                            this._setData(item, 'videoDetector');
                            break;
                        case 'CCTV':
                            this._setData(item, 'CCTVData');
                            break;
                        case 'Signal controller':
                            this._setData(item, 'signalController');
                            break;
                    }
                });
            }
        });
    }

    goToTrafficAlert() {
        this.router.navigate(['/ccm/traffic-management/traffic-alert-page']);
    }

    goToEquipmentAlarmList() {
        this.router.navigate(['/ccm/equipment-management/equipment-alarm']);
    }
}
